﻿// local variables
var currentPosition;

$(document).ready(initiateGetDrivers);

function initiateGetDrivers() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(getCurrentPositionCallback, error);
    } else {
        error('not supported');
    }
}

function getCurrentPositionCallback(position) {
    $('#status').html('obtained current location');
    currentPosition = position;
    getAllDrivers();
}

function getAllDrivers() {
    var includeAddress = $('#IncludeAddress').is(':checked');
    var dataL = {
        latitude: currentPosition.coords.latitude,
        longitude: currentPosition.coords.longitude,
        radius: 500,
        IncludeAddress: includeAddress
    };
    var url = '/Services/GetAllDriverLocationsNearMe';
    $('#status').html('getting all driver locations...');
    $.get(url, dataL, getAllDriverLocationsCallback, 'json')
        .error(function () { alert('error getting all driver locttions'); });
}

function getAllDriverLocationsCallback(data, textStatus, jqXHR) {
    var count = data.length;
    $('#status').html('success, retrieved ' + count + ' driver locations');
    displayDriversOnMap(data);
}

function displayDriversOnMap(driverLocations) {
    // Google Map
    var latlng = new google.maps.LatLng(currentPosition.coords.latitude, currentPosition.coords.longitude);
    var myOptions = {
        zoom: 16,
        center: latlng,
        mapTypeControl: false,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
    var bounds = new google.maps.LatLngBounds();

    var marker = new google.maps.Marker({
    position: latlng,
    map: map,
    title: "You are here!",
    icon: 'http://google-maps-icons.googlecode.com/files/car.png'
    });
    
    bounds.extend(latlng);

    var markers = new Array();
    for (var key in driverLocations) {
        var dloc = driverLocations[key];
        var latlng1 = new google.maps.LatLng(dloc.Latitude, dloc.Longitude);

        markers[key] = new google.maps.Marker({
            position: latlng1,
            map: map,
            title: dloc.Name
        });

        bounds.extend(latlng1);

        var html = '<b>' + dloc.Name + '</b><br />' + 'DistanceToMe: ' + dloc.DistanceToMe.toFixed(1) +
        '<br/>address:' + dloc.LocationAddress;
        attachMessage(markers[key], key, html, map);

    }
    map.fitBounds(bounds);

}

function attachMessage(marker, number, html, map) {
    var infowindow = new google.maps.InfoWindow(
      			{
      			    size: new google.maps.Size(50, 50)
      			});
    infowindow.setContent(html);
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });
}

function error(msg) {
    var s = document.querySelector('#status');
    s.innerHTML = typeof msg == 'string' ? msg : msg;
    s.className = 'fail';

    console.log(arguments);
}
